<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function index(){
        $id = I('id', 0);

    }

    /**
     * 登录
     */
    public function login()
    {
        $user_pass    = I('post.user_pass', '');
        $user_email   = I('post.user_email', '');
        $redirect_url = I('post.redirect_url', __APP__);
        $res        = array('status' => 'error', 'msg'=>'');
        if(!$user_pass) {
            $res['msg'] = '用户密码不能为空';
            $this->ajaxReturn($res);
        }
        if(!$user_email) {
            $res['msg'] = '邮箱不能为空';
            $this->ajaxReturn($res);
        }

        $user_info = array(
            'email'    => $user_email,
            'password' => $user_pass
        );
        $user_model = D('user');
        if(!$user_model->create($user_info)) {
            $res['msg'] = $user_model->getError();
            $this->ajaxReturn($res);
        } else {
            $user_id = $user_model->login($user_email,$user_pass,2);
            if($user_id < 0) {
                $res['msg'] = "登录失败";
            } else {
                $set_session = $this->set_auth_info($user_id);
                if(!$set_session) {
                    $res['msg'] = '用户写入session失败';
                } else {
                    $res = array('status' => 'succ', 'msg' => '登录成功', 'redirect_url' => $redirect_url);
                }
            }
            $this->ajaxReturn($res);
        }
    }

    /**
     * 注册新的用户
     */
    public function register()
    {
        $reg_info     = I('post.');
        $redirect_url = I('post.redirect_url', __APP__);

        $user_info = array(
            'user_name' => $reg_info['user_name'],
            'email'     => $reg_info['user_email'],
            'password'  => $reg_info['user_password']
        );
        $res = array('status' => 'error', 'msg'=>'');
        if(!check_verify($reg_info['code'])) {
            $res['msg'] = $reg_info['code'].'验证码错误';
            $this->ajaxReturn($res);
        }

        $user_model = D('user');
        $rules = array(
            array('email', '', -8, 2, 'unique'), //邮箱被占用
        );
        if(!$user_model->validate($rules)->create($user_info)) {
            $res['msg'] = $user_model->getError();
            $this->ajaxReturn($res);
        } else {
            $user_id = $user_model->add();
            if(!$user_id) {
                $res['msg'] = '创建用户失败';
                $this->ajaxReturn($res);
            } else {
                $set_session = $this->set_auth_info($user_id);
                if(!$set_session) {
                    $res['msg'] = '用户信息写入session失败';
                } else {
                    $res = array('status' => 'succ', 'msg' => '创建成功','redirect_url' => $redirect_url);
                }
                $this->ajaxReturn($res);
            }
        }
    }

    /**
     * 设置用户信息到session中，用于登录和注册完成之后
     */
    private function set_auth_info($uid)
    {
        $user_info = D('user')->info($uid);
        if(!is_array($user_info)) return false;

        $user_auth_info = array(
            'uid'       => $user_info['id'],
            'user_name' => $user_info['user_name'],
            'avatar'    => $user_info['avatar']
        );
        session('user_auth', $user_auth_info);
        session('user_auth_sign', data_auth_sign($user_auth_info));
        return true;
    }
}